/********************************************************************************
Title: Data Preparation for Kenya GE Sample
		Define time points
		Define variables to use in each index
		Define survey round to use as reference round
********************************************************************************/

use "${raw}/kenya_ge/kenya_ge_het", clear
duplicates drop
ren *_BL *
ren assets ses
tempfile desc
save `desc'


use  "${raw}/kenya_ge/raw/kenya_ge_seasonality.dta", clear
keep if !mi(hhid) & !mi(round)
isid hhid round
merge 1:1 hhid round using "${raw}/kenya_ge/kenya_ge.dta", nogen // keep(3)
merge m:1 hhid using `desc', nogen //get heterogeneity variables

foreach var of varlist female age ses {
	gen mi_`var' = mi(`var')
	bysort hhid (mi_`var'): replace `var' = `var'[1] if mi(`var')
}


loc ref first == 1

macro drop depression
gl depression  depression loneliness  hope1 

 
*-------------------------------------------------------------------------------
*			0. Formating and Renaming Variables
*-------------------------------------------------------------------------------

*			0.1		PID and HHID
*-------------------------------------------------------------------------------
cap drop pid
encode hhid, gen(pid)
bysort pid (date): gen first = _n == 1
bysort pid (date): drop if _N == 1

*			0.2		Month
*-------------------------------------------------------------------------------
*gen d = dofc(date)
*drop date 
*ren d date
gen month = month(date) + (year(date) - 2014)*12 - 8  if !mi(date)
tab month

recode month (21 = 22) (33 32 = 31) (73 = 52)
*-------------------------------------------------------------------------------
*			1. Generating Variable Lists
*-------------------------------------------------------------------------------
foreach var of varlist $depression {
	if "`var'" == "hope1" continue
	di "`var'"
	replace `var' = -1*`var'
}
codebook $depression

*tab depression
*tab loneliness
*tab hope

corr $depression

*-------------------------------------------------------------------------------
*			2. Generating Indices
*-------------------------------------------------------------------------------
foreach g in depression {
	foreach var of varlist $`g' {
	sum `var' if `ref'
	replace `var' = (`var' - `r(mean)')/`r(sd)'
	gen swt_`var' = `var'
	}
}

factor $depression if  `ref', ipf factor(1)
predict ___depression_fw


*-------------------------------------------------------------------------------
*          Unweighted indices
*-------------------------------------------------------------------------------
egen ___depression_nw = rowmean($depression)


*-------------------------------------------------------------------------------
*          Inverse-covariance-weighted indices
*-------------------------------------------------------------------------------
icw_index($depression), gen(___depression_icw)

